/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classe.controlador;

import classe.controlador.exceptions.IllegalOrphanException;
import classe.controlador.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import classe.entidade.Usuario;
import classe.entidade.Estoque;
import java.util.ArrayList;
import java.util.List;
import classe.entidade.RequisicaoItem;
import classe.entidade.RequisicaoMaterial;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Kellison
 */
public class RequisicaoMaterialJpaController implements Serializable {

    public RequisicaoMaterialJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    public RequisicaoMaterialJpaController() {
        this.emf = Persistence.createEntityManagerFactory("GerenciadorAlmoxarifadoPU");
    }

    public void create(RequisicaoMaterial requisicaoMaterial) {
        if (requisicaoMaterial.getEstoqueList() == null) {
            requisicaoMaterial.setEstoqueList(new ArrayList<Estoque>());
        }
        if (requisicaoMaterial.getRequisicaoItemList() == null) {
            requisicaoMaterial.setRequisicaoItemList(new ArrayList<RequisicaoItem>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Usuario idTblUsuario = requisicaoMaterial.getIdTblUsuario();
            if (idTblUsuario != null) {
                idTblUsuario = em.getReference(idTblUsuario.getClass(), idTblUsuario.getId());
                requisicaoMaterial.setIdTblUsuario(idTblUsuario);
            }
            List<Estoque> attachedEstoqueList = new ArrayList<Estoque>();
            for (Estoque estoqueListEstoqueToAttach : requisicaoMaterial.getEstoqueList()) {
                estoqueListEstoqueToAttach = em.getReference(estoqueListEstoqueToAttach.getClass(), estoqueListEstoqueToAttach.getId());
                attachedEstoqueList.add(estoqueListEstoqueToAttach);
            }
            requisicaoMaterial.setEstoqueList(attachedEstoqueList);
            List<RequisicaoItem> attachedRequisicaoItemList = new ArrayList<RequisicaoItem>();
            for (RequisicaoItem requisicaoItemListRequisicaoItemToAttach : requisicaoMaterial.getRequisicaoItemList()) {
                requisicaoItemListRequisicaoItemToAttach = em.getReference(requisicaoItemListRequisicaoItemToAttach.getClass(), requisicaoItemListRequisicaoItemToAttach.getId());
                attachedRequisicaoItemList.add(requisicaoItemListRequisicaoItemToAttach);
            }
            requisicaoMaterial.setRequisicaoItemList(attachedRequisicaoItemList);
            em.persist(requisicaoMaterial);
            if (idTblUsuario != null) {
                idTblUsuario.getRequisicaoMaterialList().add(requisicaoMaterial);
                idTblUsuario = em.merge(idTblUsuario);
            }
            for (Estoque estoqueListEstoque : requisicaoMaterial.getEstoqueList()) {
                RequisicaoMaterial oldIdRequisicaoOfEstoqueListEstoque = estoqueListEstoque.getIdRequisicao();
                estoqueListEstoque.setIdRequisicao(requisicaoMaterial);
                estoqueListEstoque = em.merge(estoqueListEstoque);
                if (oldIdRequisicaoOfEstoqueListEstoque != null) {
                    oldIdRequisicaoOfEstoqueListEstoque.getEstoqueList().remove(estoqueListEstoque);
                    oldIdRequisicaoOfEstoqueListEstoque = em.merge(oldIdRequisicaoOfEstoqueListEstoque);
                }
            }
            for (RequisicaoItem requisicaoItemListRequisicaoItem : requisicaoMaterial.getRequisicaoItemList()) {
                RequisicaoMaterial oldIdTblRequisicaoMaterialOfRequisicaoItemListRequisicaoItem = requisicaoItemListRequisicaoItem.getIdTblRequisicaoMaterial();
                requisicaoItemListRequisicaoItem.setIdTblRequisicaoMaterial(requisicaoMaterial);
                requisicaoItemListRequisicaoItem = em.merge(requisicaoItemListRequisicaoItem);
                if (oldIdTblRequisicaoMaterialOfRequisicaoItemListRequisicaoItem != null) {
                    oldIdTblRequisicaoMaterialOfRequisicaoItemListRequisicaoItem.getRequisicaoItemList().remove(requisicaoItemListRequisicaoItem);
                    oldIdTblRequisicaoMaterialOfRequisicaoItemListRequisicaoItem = em.merge(oldIdTblRequisicaoMaterialOfRequisicaoItemListRequisicaoItem);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(RequisicaoMaterial requisicaoMaterial) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RequisicaoMaterial persistentRequisicaoMaterial = em.find(RequisicaoMaterial.class, requisicaoMaterial.getId());
            Usuario idTblUsuarioOld = persistentRequisicaoMaterial.getIdTblUsuario();
            Usuario idTblUsuarioNew = requisicaoMaterial.getIdTblUsuario();
            List<Estoque> estoqueListOld = persistentRequisicaoMaterial.getEstoqueList();
            List<Estoque> estoqueListNew = requisicaoMaterial.getEstoqueList();
            List<RequisicaoItem> requisicaoItemListOld = persistentRequisicaoMaterial.getRequisicaoItemList();
            List<RequisicaoItem> requisicaoItemListNew = requisicaoMaterial.getRequisicaoItemList();
            List<String> illegalOrphanMessages = null;
            for (RequisicaoItem requisicaoItemListOldRequisicaoItem : requisicaoItemListOld) {
                if (!requisicaoItemListNew.contains(requisicaoItemListOldRequisicaoItem)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain RequisicaoItem " + requisicaoItemListOldRequisicaoItem + " since its idTblRequisicaoMaterial field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (idTblUsuarioNew != null) {
                idTblUsuarioNew = em.getReference(idTblUsuarioNew.getClass(), idTblUsuarioNew.getId());
                requisicaoMaterial.setIdTblUsuario(idTblUsuarioNew);
            }
            List<Estoque> attachedEstoqueListNew = new ArrayList<Estoque>();
            for (Estoque estoqueListNewEstoqueToAttach : estoqueListNew) {
                estoqueListNewEstoqueToAttach = em.getReference(estoqueListNewEstoqueToAttach.getClass(), estoqueListNewEstoqueToAttach.getId());
                attachedEstoqueListNew.add(estoqueListNewEstoqueToAttach);
            }
            estoqueListNew = attachedEstoqueListNew;
            requisicaoMaterial.setEstoqueList(estoqueListNew);
            List<RequisicaoItem> attachedRequisicaoItemListNew = new ArrayList<RequisicaoItem>();
            for (RequisicaoItem requisicaoItemListNewRequisicaoItemToAttach : requisicaoItemListNew) {
                requisicaoItemListNewRequisicaoItemToAttach = em.getReference(requisicaoItemListNewRequisicaoItemToAttach.getClass(), requisicaoItemListNewRequisicaoItemToAttach.getId());
                attachedRequisicaoItemListNew.add(requisicaoItemListNewRequisicaoItemToAttach);
            }
            requisicaoItemListNew = attachedRequisicaoItemListNew;
            requisicaoMaterial.setRequisicaoItemList(requisicaoItemListNew);
            requisicaoMaterial = em.merge(requisicaoMaterial);
            if (idTblUsuarioOld != null && !idTblUsuarioOld.equals(idTblUsuarioNew)) {
                idTblUsuarioOld.getRequisicaoMaterialList().remove(requisicaoMaterial);
                idTblUsuarioOld = em.merge(idTblUsuarioOld);
            }
            if (idTblUsuarioNew != null && !idTblUsuarioNew.equals(idTblUsuarioOld)) {
                idTblUsuarioNew.getRequisicaoMaterialList().add(requisicaoMaterial);
                idTblUsuarioNew = em.merge(idTblUsuarioNew);
            }
            for (Estoque estoqueListOldEstoque : estoqueListOld) {
                if (!estoqueListNew.contains(estoqueListOldEstoque)) {
                    estoqueListOldEstoque.setIdRequisicao(null);
                    estoqueListOldEstoque = em.merge(estoqueListOldEstoque);
                }
            }
            for (Estoque estoqueListNewEstoque : estoqueListNew) {
                if (!estoqueListOld.contains(estoqueListNewEstoque)) {
                    RequisicaoMaterial oldIdRequisicaoOfEstoqueListNewEstoque = estoqueListNewEstoque.getIdRequisicao();
                    estoqueListNewEstoque.setIdRequisicao(requisicaoMaterial);
                    estoqueListNewEstoque = em.merge(estoqueListNewEstoque);
                    if (oldIdRequisicaoOfEstoqueListNewEstoque != null && !oldIdRequisicaoOfEstoqueListNewEstoque.equals(requisicaoMaterial)) {
                        oldIdRequisicaoOfEstoqueListNewEstoque.getEstoqueList().remove(estoqueListNewEstoque);
                        oldIdRequisicaoOfEstoqueListNewEstoque = em.merge(oldIdRequisicaoOfEstoqueListNewEstoque);
                    }
                }
            }
            for (RequisicaoItem requisicaoItemListNewRequisicaoItem : requisicaoItemListNew) {
                if (!requisicaoItemListOld.contains(requisicaoItemListNewRequisicaoItem)) {
                    RequisicaoMaterial oldIdTblRequisicaoMaterialOfRequisicaoItemListNewRequisicaoItem = requisicaoItemListNewRequisicaoItem.getIdTblRequisicaoMaterial();
                    requisicaoItemListNewRequisicaoItem.setIdTblRequisicaoMaterial(requisicaoMaterial);
                    requisicaoItemListNewRequisicaoItem = em.merge(requisicaoItemListNewRequisicaoItem);
                    if (oldIdTblRequisicaoMaterialOfRequisicaoItemListNewRequisicaoItem != null && !oldIdTblRequisicaoMaterialOfRequisicaoItemListNewRequisicaoItem.equals(requisicaoMaterial)) {
                        oldIdTblRequisicaoMaterialOfRequisicaoItemListNewRequisicaoItem.getRequisicaoItemList().remove(requisicaoItemListNewRequisicaoItem);
                        oldIdTblRequisicaoMaterialOfRequisicaoItemListNewRequisicaoItem = em.merge(oldIdTblRequisicaoMaterialOfRequisicaoItemListNewRequisicaoItem);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = requisicaoMaterial.getId();
                if (findRequisicaoMaterial(id) == null) {
                    throw new NonexistentEntityException("The requisicaoMaterial with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RequisicaoMaterial requisicaoMaterial;
            try {
                requisicaoMaterial = em.getReference(RequisicaoMaterial.class, id);
                requisicaoMaterial.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The requisicaoMaterial with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<RequisicaoItem> requisicaoItemListOrphanCheck = requisicaoMaterial.getRequisicaoItemList();
            for (RequisicaoItem requisicaoItemListOrphanCheckRequisicaoItem : requisicaoItemListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This RequisicaoMaterial (" + requisicaoMaterial + ") cannot be destroyed since the RequisicaoItem " + requisicaoItemListOrphanCheckRequisicaoItem + " in its requisicaoItemList field has a non-nullable idTblRequisicaoMaterial field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Usuario idTblUsuario = requisicaoMaterial.getIdTblUsuario();
            if (idTblUsuario != null) {
                idTblUsuario.getRequisicaoMaterialList().remove(requisicaoMaterial);
                idTblUsuario = em.merge(idTblUsuario);
            }
            List<Estoque> estoqueList = requisicaoMaterial.getEstoqueList();
            for (Estoque estoqueListEstoque : estoqueList) {
                estoqueListEstoque.setIdRequisicao(null);
                estoqueListEstoque = em.merge(estoqueListEstoque);
            }
            em.remove(requisicaoMaterial);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<RequisicaoMaterial> findRequisicaoMaterialEntities() {
        return findRequisicaoMaterialEntities(true, -1, -1);
    }

    public List<RequisicaoMaterial> findRequisicaoMaterialEntities(int maxResults, int firstResult) {
        return findRequisicaoMaterialEntities(false, maxResults, firstResult);
    }

    private List<RequisicaoMaterial> findRequisicaoMaterialEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(RequisicaoMaterial.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public RequisicaoMaterial findRequisicaoMaterial(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(RequisicaoMaterial.class, id);
        } finally {
            em.close();
        }
    }

    public int getRequisicaoMaterialCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<RequisicaoMaterial> rt = cq.from(RequisicaoMaterial.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
